﻿    //jquery.validate表单验证
    $(document).ready(function () {
        //登陆表单验证
        $("#form").validate({
            rules: {
                Title: {
                    required: true,//必填
                    minlength: 3, //最少3个字符
                    maxlength: 30,//最多32个字符
                },
                DomainName: {
                    required: true,
                    minlength: 3,
                    maxlength: 30,
                },
                Keywords: {
                    required: true,
                    minlength: 1,
                    maxlength: 500,
                },
                Description:{
                    required: true,
                    minlength: 3,
                    maxlength: 500,
                },
                Contact: {
                    required: true,
                    minlength: 2,
                    maxlength: 30,
                },
                MobilePhone: {
                    required: true,
                    MobilePhone: true,//自定义的规则
                    digits: true,//整数
                },
                Phone: {
                    required: true,
                    Phone: true,//自定义的规则
                },
                QQ: {
                    required: true
                },
                Email: {
                    required: true,
                    email: true
                },
                Address: {
                    required: true,
                    minlength: 3,
                    maxlength: 30,
                }
            },
            //错误信息提示
            messages: {
                Title: {
                    required: "必须填写网站标题",
                    minlength: "账号至少为3个字符",
                    maxlength: "账号至多为30个字符",
                },
                DomainName: {
                    required: "必须填写网站域名",
                    minlength: "密码至少为3个字符",
                    maxlength: "密码至多为30个字符",
                },
                Keywords: {
                    required: "必须填写网站关键字",
                    minlength: "密码至少为1个字符",
                    maxlength: "密码至多为500个字符",
                },
                Description: {
                    required: "必须填写网站描述",
                    minlength: "密码至少为3个字符",
                    maxlength: "密码至多为500个字符",
                },
                Contact: {
                    required: "必须填写联系人",
                    minlength: "密码至少为2个字符",
                    maxlength: "密码至多为30个字符",
                },
                MobilePhone: {
                    required: "必须填写手机",
                    digits: "请输入正确的手机号码",
                },
                Phone: {
                    required: "必须填写电话",
                    minlength: "账号至少为3个字符",
                    maxlength: "账号至多为30个字符",
                },
                QQ: {
                    required: "必须填写QQ",
                    minlength: "账号至少为3个字符",
                    maxlength: "账号至多为30个字符",
                },
                Email: {
                    required: "必须填写邮箱",
                    minlength: "账号至少为3个字符",
                    maxlength: "账号至多为30个字符",
                },
                Address: {
                    required: "必须填写地址",
                    minlength: "账号至少为3个字符",
                    maxlength: "账号至多为30个字符",
                }
            },
            submitHandler: function (form) {
                $.post("/Manage/Home/Add", $(form).serialize(), function (rv) {
                    if (rv.State) {
                        return window.location.href = "/Manage/Home/Index";
                    } else {
                        Alert(rv.Message);
                    }
                });
            }
        });
        //添加自定义验证规则
        jQuery.validator.addMethod("MobilePhone", function (value, element) {
            var length = value.length;
            var MobilePhone = /^1[3|4|5|8][0-9]\d{4,8}$/;
            return this.optional(element) || (length == 11 && MobilePhone.test(value));
        }, "手机号码格式错误");
        
        //添加自定义验证规则
        jQuery.validator.addMethod("Phone", function (value, element) {
            var length = value.length;
            var Phone = /^0\d{2,3}-?\d{7,8}$/;
            return this.optional(element) || (length == 12 || length == 13 && Phone.test(value));
        }, "电话格式错误");     
    });
